﻿using Sc.Domain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Persistence
{
    public class TrainDetailDAO
    {
        #region  BasicMethod

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(TrainDetail model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into TrainDetails(");
            strSql.Append("TrainId,Driver,Score)");
            strSql.Append(" values (");
            strSql.Append("@TrainId,@Driver,@Score)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@TrainId", SqlDbType.Int,4),
					new SqlParameter("@Driver", SqlDbType.Int,4),
					new SqlParameter("@Score", SqlDbType.Decimal,5)};
            parameters[0].Value = model.TrainId;
            parameters[1].Value = model.Driver;
            parameters[2].Value = model.Score;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 执行多条数据
        /// </summary>
        /// <param name="list"></param>
        /// <param name="trainId">主信息ID</param>
        /// <returns></returns>
        public int Add(IList<TrainDetail> list, int trainId)
        {
            IList<HelperModel> result = new List<HelperModel>();
            foreach (var item in list)
            {
                HelperModel ent = new HelperModel();
                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into TrainDetails(");
                strSql.Append("TrainId,Driver,Score)");
                strSql.Append(" values (");
                strSql.Append("@TrainId,@Driver,@Score)");
                ent.Sql = strSql.ToString();
                SqlParameter[] parameters = {
					new SqlParameter("@TrainId", SqlDbType.Int,4),
					new SqlParameter("@Driver", SqlDbType.Int,4),
					new SqlParameter("@Score", SqlDbType.Decimal,5)};
                parameters[0].Value = trainId;
                parameters[1].Value = item.Driver;
                parameters[2].Value = item.Score;
                ent.Parameter = parameters;
                result.Add(ent);
            }
            return DbHelperSQL.ExecuteSqlTran(result);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(TrainDetail model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update TrainDetails set ");
            strSql.Append("TrainId=@TrainId,");
            strSql.Append("Driver=@Driver,");
            strSql.Append("Score=@Score");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@TrainId", SqlDbType.Int,4),
					new SqlParameter("@Driver", SqlDbType.Int,4),
					new SqlParameter("@Score", SqlDbType.Decimal,5),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.TrainId;
            parameters[1].Value = model.Driver;
            parameters[2].Value = model.Score;
            parameters[3].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from TrainDetails ");
            strSql.Append(" where TrainId=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from TrainDetails ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 获取明细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IList<TrainDetail> GetList(int id)
        {
            string sql = "SELECT * FROM Vw_TrainDetails WHERE TrainId=@TrainId";
            SqlParameter[] param = new SqlParameter[]{
                new SqlParameter("@TrainId",SqlDbType.Int)
            };
            param[0].Value = id;
            IList<TrainDetail> list = new List<TrainDetail>();
            using (SqlDataReader dr = DbHelperSQL.ExecuteReader(sql, param))
            {
                while (dr.Read())
                {
                    try
                    {
                        TrainDetail obj = new TrainDetail();
                        obj.Id = (dr["Id"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("Id")));
                        obj.Name = (dr["Name"] == System.DBNull.Value ? string.Empty : dr.GetString(dr.GetOrdinal("Name")));
                        obj.Score = (dr["Score"] == System.DBNull.Value ? 0M : dr.GetDecimal(dr.GetOrdinal("Score")));
                        list.Add(obj);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("从DataReader中读取数据失败，可能与相关的字段不存在有关！请检查！||" + ex.Message + "||" + ex.Source);
                    }
                }
                dr.Close();
            }
            return list;
        }
        #endregion  BasicMethod

    }
}
